<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/vue.js"></script>
  <style>
    .success{
      background-color:lightGreen;
      color:green;
      border:green 1px solid ;
    }
    .fail{
      background-color:pink;
      color:red;
      border:red 1px solid;
    }
  </style>
</head>
<body>
  <div id="app">
    手机号:<input type="text" v-model="phone">
        <span :class="phoneObj">{{phoneMsg}}</span><br>
    密码:<input type="password" v-model="pwd">
        <span :class="pwdObj">{{pwdMsg}}</span>
  </div>
  <script>
    //2. 创建new Vue()对象
    new Vue({
      el:"#app",
      data:{
        phone:"",
        phoneObj:{
          success:false,
          fail:false
        },
        phoneMsg:"",

        pwd:"",
        pwdObj:{
          success:false,
          fail:false
        },
        pwdMsg:""
      },
      watch:{
        phone(){
          var reg=/^1[3-9]\d{9}$/
          var result=reg.test(this.phone);
          if(result==true){
            this.phoneObj={
            //启用success class，不启用fail class
              success:true, fail:false
            }
            this.phoneMsg="手机号格式正确";
          }else{
            this.phoneObj={
            //不启用success class，启用fail class
              success:false, fail:true
            }
            this.phoneMsg="手机号格式不正确";
          }
        },
        pwd(){
          var reg=/^\d{6}$/
          var result=reg.test(this.pwd);
          if(result==true){
            this.pwdObj={
            //启用success class，不启用fail class
              success:true, fail:false
            }
            this.pwdMsg="密码格式正确";
          }else{
            this.pwdObj={
            //不启用success class，启用fail class
              success:false, fail:true
            }
            this.pwdMsg="密码格式不正确";
          }
        }
      }
    })
  </script>
</body>
</html>
